home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
dskut
/
chop1292.zip
/
CHOP.C
< prev
next >
Wrap
C/C++ Source or Header
|
1992-12-19
|
19KB
|
555 lines
/* This program splits the input file into smaller files.
USAGE: CHOP FILENAME SIZE [OUTPATH] -SWITCHES
Version 1.23. Written by W. J. Kennamer and released into the public domain.
Enhanced by Edgar Swank " " "
Enhancements/Changes:
1)Performance improvement by reading/writing in large blocks.
2)Support for non-ASCII files with -b option.
3)Add optional disk/path for output files.
4)Add -w option to wait after each output file (except last).
Note: this source is now for IBM/MS C Compiler.
Compile with HUGE memory model /AH.
FILENAME is any valid MS-DOS filename. Wildcards are not supported.
Output file names will be FILENAME.1, FILENAME.2, etc.
Output files are always terminated with a carriage return and
a <ctrl>Z, UNLESS -b is specified.
SIZE is the desired file size for each new file.
OUTPATH is an optional disk/pathname (A: or \path\ or a:\path\)
to put output files on a different disk and/or directory from input file.
If using -p with OUTPATH, enter some dummy argument for SIZE.
SWITCHES must follow the FILENAME,SIZE and OUTPATH parameters,
and may be entered in any order. Switches may be combined.
Valid SWITCHES are:
-b binary ║ do not add cr/lf/^z to output files.
║ forces -s.
-s strict ║ chop file at exact SIZE boundary.
-w wait ║ wait after closing each output file.
║ can change output diskette if separate from input.
-r return ║ chop file at first carriage return
║ following SIZE characters (default).
-px partition ║ partition file into x equal units.
║ SIZE is ignored (and may be omitted) if
║ you choose this option.
File pointer fp1 is the input file. fp2 is always the output
file, though its name changes as new output files are
opened.
*/
#include <fcntl.h>
#include <dos.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>
#define VERSION "1.23"
#define PROGNAME "CHOP"
#define CTRL_Z 0x1a
#define VOID int
#define CR 0x0d
#define LF 0x0a
#define OFF 0
#define ON 1
int binary = OFF; /* do not add CR,LF,EOF */
int wait = OFF; /* Wait after each output file close*/
int count; /* no. of digits in size */
int c; /* current character */
int j; /* index */
long inpleft; /* Bytes input left to read */
long inplimit; /* Bytes input left to read low lim */
long bfsiz; /* I/O buffer size */
long bread; /* Bytes read into buffer */
long pbread; /* Bytes read this partition */
long bwrit; /* Bytes written from buffer */
long pbwrit; /* Bytes written this partition */
long size = 0; /* partition value -- file size */
long byte_count = 0; /* total bytes in original file */
long inpfz = 0; /* total bytes in original file */
long out_file_size = 0; /* total bytes in output file */
char *bfpa[20]; /* I/O buffer pointer array */
FILE *fp1,*fp2; /* fp1=input, fp2=output */
int derno,erno;
int bfx; /* buffer array index */
int num_output_files = 0; /* no of files successfully created */
int length; /* length of matching string */
int normal = ON; /* normal CR breaks */
int strict = OFF; /* break exactly at boundary */
int partition = OFF; /* strict or normal */
int num_part; /* number of partitions */
int int_size; /* integer for file size */
int nmb; /* number of 16k blocks IO */
unsigned int w;
char *p; /* pointer to switch string */
char *period_p; /* location of period in filename */
char *output_file; /* output file name */
char filename[80]; /* original file name */
char fnwork[80]; /* workarea for file name */
char outpath[64]; /* optional output disk/path */
char line1[80]; /* data buffer */
char line2[30]; /* data buffer */
char *switches; /* string to hold switches */
char infile[80]; /* input filename */
VOID help();
VOID close_out();
VOID block_read();
VOID block_write();
VOID copy_to_eol();
VOID construct_output_filename();
VOID delete_file();
main(argc,argv)
int argc;
char *argv[];
{
/* test code to prints args **********
printf("ARGC=%d\n",argc);
for (j=0;j<argc ;j++ )
{
printf("ARGV[%d]=%s\n",j,argv[j]);
}
***************************************/
bfsiz=0;
for (nmb=1;nmb<=20 ;nmb++ )
{
bfpa[nmb-1]=malloc(16384);
if (bfpa[nmb-1]==NULL)
{
break;
}
else
{
bfsiz=(long)nmb*16384;
}
} /*for (nmb=1;nmb<=10 ;nmb++ )*/
nmb--;
if (nmb==0)
{
printf ("Insufficient Memory for I/O Buffer.\n");
exit(8);
}
else
printf ("%ld (%u*16384) bytes allocated for I/O Buffers.\n",bfsiz,nmb);
if( argc > 5 || argc < 3 || strcmp(argv[2],"-?")==0
|| strcmp(argv[3],"-?")==0
|| strcmp(argv[4],"-?")==0 )
{
help();
exit(1);
}
/* see which argument is the switch */
switches = line2;
strcpy(switches,"");
for (j=2;j<argc;j++)
{
if( argv[j][0]=='-')
{
strcpy(switches,argv[j]);
break;
}
} /*for*/
if (j>3)
strcpy(outpath,argv[3]);
else
outpath[0]=0;
set_switches();
if( (fp1 = fopen(argv[1],"rb") ) == NULL)
{
printf("Cannot open %s for input.\n",argv[1]);
exit(1);
}
if( fseek(fp1,0L,2) == -1) /* position at EOF */
{
printf("Error seeking end of input file.\n");
exit(1);
}
inpfz = ftell(fp1); /* tell EOF */
rewind(fp1); /* back to beginning of file */
printf(" Input file size = %ld bytes\n",inpfz);
/* determine file size */
if(partition == ON)
{
/* each partition requires 3 extra bytes -- CTRL_Z, newline */
size = ((inpfz + 3*(long)num_part) / (long)num_part ) + 1 ;
printf(" Partition size = %ld bytes\n",size);
printf("No. of partitions = %ld\n\n",(long)num_part);
}
else /* partition is not ON */
{ /* read SIZE from command line */
count = sscanf(argv[2],"%D",&size); /* convert string to long integer */
if(count == 0)
{
printf("Invalid size\n");
exit(1);
}
} /*else*/
if (size>inpfz)
{
printf("Requested Size > Input File Size.\n");
exit(1);
}
strcpy(filename,argv[1]);
strcpy(infile,argv[1]);
period_p = strchr(filename,'.'); /* locate the period, if any */
if( period_p != NULL )
*period_p = '\0'; /* chop off the period */
if (outpath[0]!=0)